package contract

import (
	"context"
	"fmt"
	model "service/model/hr"
	"service/model/mod"

	gpb "gitee.com/cloudapex/protos/hr/pb"

	"gitee.com/cloudapex/ulern/htp"
	"github.com/gin-gonic/gin"
)

/*
 * 描述: 员工合同列表
 * 路由: contract/getContractList
 * 方法: GET
 *********************************************************************/
type ContractList struct {
	Phone      string `form:"phone" json:"phone"`                       //手机号
	WorkId     int64  `form:"workId" json:"workId" binding:"omitempty"` //员工ID
	NumberId   string `form:"numberId" json:"numberId"`                 //身份证号
	Name       string `form:"userName" json:"userName"`                 //姓名
	WId        int64  `form:"wId" json:"wId"`                           //岗位ID
	State      int64  `form:"inState" json:"inState"`                   //在职状态
	InAt       int64  `form:"inAt" json:"inAt"`                         //入职时间
	FirstState int64  `form:"firstState" json:"firstState"`             //是否签署合同
	PoFraId    int64  `form:"poFraId" json:"poFraId"`                   //员工岗位组织架构（搜索条件）
	TimeReq    []struct {
		Tag   int64 `form:"tag" json:"tag"`         //1入职时间 2社保缴费时间 3合同时间 4 离职时间 5转正时间
		Start int64 `form:"startAt" json:"startAt"` //开始时间
		End   int64 `form:"endAt" json:"endAt"`     //结束时间
	} `form:"timeRep" json:"timeRep"`
	Page  int64 `form:"page" json:"page"`   //页码
	Count int64 `form:"count" json:"count"` //总数
}

func (this *ContractList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	jwtUser := mod.CurrUser(c)
	fmt.Println("对象", this.TimeReq)
	timeReq := make([]*gpb.TimeReq, 0)
	if len(this.TimeReq) > 0 {
		for _, v := range this.TimeReq {
			timePara := new(gpb.TimeReq)
			timePara.Tag = v.Tag
			timePara.Start = v.Start
			timePara.End = v.End
			timeReq = append(timeReq, timePara)
		}
	}
	fmt.Println(timeReq)
	rsp, err := model.StaffServer.GetContractList(ctx, &gpb.ContractReq{
		Count: this.Count,
		Page:  this.Page,
		BaseReq: &gpb.HrBaseReq{CId: jwtUser.CId,
			Phone:      this.Phone,
			WorkId:     this.WorkId,
			NumberName: this.Name,
			NumberId:   this.NumberId,
			WId:        this.WId,
			PoFraId:    this.PoFraId,
		},
		InOutReq: &gpb.InOutReq{
			InAt:    this.InAt,
			State:   this.State,
			TimeReq: timeReq,
		},
		StafCont: &gpb.StaffContract{
			State: this.FirstState,
		},
	})
	if err != nil {
		return htp.RespModelErr("列表信息加载有误", err)
	}

	return htp.RespOK("", rsp)
}

/*
 * 描述: 修改多个员工合同
 * 路由: contract/updateContract
 * 方法: POST
 *********************************************************************/
type UpdateContract struct {
	ContType int64 `form:"contType" json:"contType" binding:"required"`   //合同类型 1.电子 2.纸质
	ContAt   int64 `form:"contAt" json:"contAt" binding:"required"`       //签订时间
	ConState int64 `form:"contState" json:"contState" binding:"required"` //1 未签署 2签署
	ContrId  []struct {
		UserId int64 `form:"userId" json:"userId" binding:"required"` //用户ID
		Id     int64 `form:"conId" json:"conId" binding:"required"`   //合同ID
	} `json:"contrId"` //合同信息
}

func (this *UpdateContract) Handle(c *gin.Context, ctx context.Context) htp.Response {
	jwtUser := mod.CurrUser(c)
	contReq := make([]*gpb.BaseUInfo, 0)

	//批量修改多个员工的合同
	if len(this.ContrId) > 0 {
		for _, v := range this.ContrId {
			contPara := new(gpb.BaseUInfo)
			contPara.Hid = v.UserId
			contPara.Id = v.Id
			contReq = append(contReq, contPara)
		}
		_, err := model.StaffServer.EditContract(ctx, &gpb.StaffContractReq{
			OperId:     jwtUser.UId,
			BaseUInfos: contReq,
			State:      this.ConState,
			ContType:   this.ContType,
			ContAt:     this.ContAt,
		})
		if err != nil {
			return htp.RespModelErr("更新合同信息失败", err)
		}
	}
	return htp.RespOK("", nil)
}

/*
 * 描述: 员工社保列表
 * 路由: contract/getSocialList
 * 方法: GET
 *********************************************************************/
type SocialList struct {
	Phone      string `form:"phone" json:"phone"`           //手机号
	WorkId     int64  `form:"workId" json:"workId"`         //员工ID
	Name       string `form:"userName" json:"userName"`     //姓名
	NumberId   string `form:"numberId" json:"numberId"`     //身份证号
	WId        int64  `form:"wId" json:"wId"`               //岗位ID
	State      int64  `form:"inState" json:"inState"`       //在职状态
	InAt       int64  `form:"inAt" json:"inAt"`             //入职时间
	SocAt      int64  `form:"socAt" json:"socAt"`           //社保期缴日期
	FirstState int64  `form:"firstState" json:"firstState"` //是否已交合同
	PoFraId    int64  `form:"poFraId" json:"poFraId"`       //岗位架构id(用于搜索)
	TimeReq    []struct {
		Tag   int64 `form:"tag" json:"tag"`         //1入职时间 2社保缴费时间 3合同时间 4 离职时间 5转正时间
		Start int64 `form:"startAt" json:"startAt"` //开始时间
		End   int64 `form:"endAt" json:"endAt"`     //结束时间
	} `form:"timeRep" json:"timeRep"`
	Page  int64 `form:"page" json:"page"`   //页码
	Count int64 `form:"count" json:"count"` //总数
}

func (this *SocialList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	jwtUser := mod.CurrUser(c)
	fmt.Println("对象", this.TimeReq)
	timeReq := make([]*gpb.TimeReq, 0)
	if len(this.TimeReq) > 0 {
		for _, v := range this.TimeReq {
			timePara := new(gpb.TimeReq)
			timePara.Tag = v.Tag
			timePara.Start = v.Start
			timePara.End = v.End
			timeReq = append(timeReq, timePara)
		}
	}
	rsp, err := model.StaffServer.GetInsuraList(ctx, &gpb.InsuratReq{
		Count: this.Count,
		Page:  this.Page,
		BaseReq: &gpb.HrBaseReq{CId: jwtUser.CId,
			Phone:      this.Phone,
			NumberName: this.Name,
			NumberId:   this.NumberId,
			WorkId:     this.WorkId,
			WId:        this.WId,
			PoFraId:    this.PoFraId,
		},
		InOutReq: &gpb.InOutReq{
			InAt:    this.InAt,
			State:   this.State,
			TimeReq: timeReq,
		},
		StaffInsu: &gpb.StaffInsurance{
			State: this.FirstState,
		},
	})
	if err != nil {
		return htp.RespModelErr("员工社保列表加载有误", err)
	}

	return htp.RespOK("", rsp)
}

/*
 * 描述: 修改多个员社保
 * 路由: contract/updateSocial
 * 方法: POST
 *********************************************************************/
type UpdateSocial struct {
	SocCity  string `form:"socCity" json:"socCity" binding:"required"`   //社保城市
	SocAt    int64  `form:"socAt" json:"socAt" binding:"required"`       //缴费时间
	SocState int64  `form:"socState" json:"socState" binding:"required"` //1 未签署 2签署
	SocialId []struct {
		UserId int64 `form:"userId" json:"userId" binding:"required"` //用户ID
		Id     int64 `form:"socialId" json:"socialId"`                //社保ID
	} `json:"socialId"`
}

func (this *UpdateSocial) Handle(c *gin.Context, ctx context.Context) htp.Response {
	jwtUser := mod.CurrUser(c)
	contReq := make([]*gpb.BaseUInfo, 0)
	//批量修改多个员工的合同
	if len(this.SocialId) > 0 {
		for _, v := range this.SocialId {
			contPara := new(gpb.BaseUInfo)
			contPara.Hid = v.UserId
			contPara.Id = v.Id
			contReq = append(contReq, contPara)
		}
		_, err := model.StaffServer.EditInsurance(ctx, &gpb.StaffInsuranceReq{
			BaseUInfos: contReq,
			State:      this.SocState,
			City:       this.SocCity,
			At:         this.SocAt,
			OperId:     jwtUser.UId,
		})
		fmt.Println(err)
		if err != nil {
			return htp.RespModelErr("更新社保信息失败", err)
		}
	}
	return htp.RespOK("", nil)
}

/*
 * 描述: 员工离职列表
 * 路由: contract/getSocialList
 * 方法: GET
 *********************************************************************/
type StaffQuitList struct {
	Page  int64 `form:"page" json:"page"`   //页码
	Count int64 `form:"count" json:"count"` //总数
}

func (this *StaffQuitList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	jwtUser := mod.CurrUser(c)
	rsp, err := model.StaffServer.QuitUserList(ctx, &gpb.HrDetailReq{
		Count:    this.Count,
		Page:     this.Page,
		BaseReq:  &gpb.HrBaseReq{CId: jwtUser.CId},
		InOutReq: &gpb.InOutReq{},
	})
	if err != nil {
		return htp.RespModelErr("列表信息加载有误！", err)
	}

	return htp.RespOK("", rsp)
}

/*
 * 描述: 获取离职题库的列表
 * 路由: contract/getSocialList
 * 方法: GET
 *********************************************************************/
type QuitToplibList struct {
	CId int64 `form:"cId" json:"cId" binding:"required"` //公司ID
}

func (this *QuitToplibList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	rsp, err := model.StaffServer.GetQuitList(ctx, &gpb.BaseReq{
		CId: this.CId,
	})
	if err != nil {
		return htp.RespModelErr("列表信息加载有误！", err)
	}

	return htp.RespOK("", rsp)
}

/*
 * 描述: 重置員工信息
 * 路由: contract/setRegStatus
 * 方法: POST
 *********************************************************************/

type SetRegStatus struct {
	UserId int64 `form:"userId" json:"userId" binding:"required"` //用戶ID
}

func (this *SetRegStatus) Handle(c *gin.Context, ctx context.Context) htp.Response {
	_, err := model.StaffServer.SetStaffMod(ctx, &gpb.StffModReq{
		UserId: this.UserId,
		Status: 1,
	})
	fmt.Println(err)
	if err != nil {
		return htp.RespModelErr("更新员工基本信息失败", err)
	}
	return htp.RespOK("", nil)
}

/*
 * 描述: 离职做题记录
 * 路由: contract/setRegStatus
 * 方法: POST
 *********************************************************************/
type QuitToplibCommit struct {
	Opinion string `form:"reMarks" json:"reMarks"` //离职建议
	TopLib  struct {
		QuitReq []struct {
			UserId   int64   `form:"userId" json:"userId"` //员工ID
			ProId    int64   `form:"proId" json:"proId"`   //题干ID
			SelectId []int64 `json:"selectId"`
		} `json:"quitReq"`
	} `json:"topLib"`
}

func (this *QuitToplibCommit) Handle(c *gin.Context, ctx context.Context) htp.Response {
	quitRs := make([]*gpb.QuitPara, 0)
	for _, v := range this.TopLib.QuitReq {
		quitPara := new(gpb.QuitPara)
		quitPara.Hid = v.UserId
		quitPara.ProId = v.ProId
		quitRs = append(quitRs, quitPara)
		for _, i := range v.SelectId {
			quitPara.SelectId = append(quitPara.SelectId, i)
		}
	}
	quitReq := new(gpb.QuitReq)
	quitReq.Opinion = this.Opinion
	quitReq.QuitParas = quitRs
	rsp, err := model.StaffServer.QuitCommit(ctx, quitReq)
	if err != nil {
		return htp.RespModelErr("信息有误", err)
	}

	return htp.RespOK("", rsp)
}

/*
 * 描述: 获取题干列表
 * 路由: group/api
 * 方法: GET
 *********************************************************************/
type TopLibList struct {
	CId int64 `form:"cId" json:"cId" binding:"required"` //公司ID
}
type TopLibCount struct {
	Id    int64  `json:"qId"`      //ID
	Name  string `json:"topName"`  //名称
	Count int64  `json:"topCount"` //总数
}

func (this *TopLibList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	list := make([]TopLibCount, 0)
	rspLib, err := model.StaffServer.GetProblemList(ctx, &gpb.BasePageReq{
		CId: this.CId,
	})
	if err != nil {
		return htp.RespModelErr("", err)
	}

	if len(rspLib.ProbleList) > 0 {
		for _, v := range rspLib.ProbleList {
			rsp, err := model.StaffServer.GetPoolCount(ctx, &gpb.BaseCountReq{
				Id:   v.Id,
				CId:  this.CId,
				Type: 1,
			})
			if err != nil {
				return htp.RespModelErr(rsp.Message, err)
			}
			var topReq TopLibCount
			topReq.Count = rsp.Count
			topReq.Id = v.Id
			topReq.Name = v.Name
			list = append(list, topReq)
		}
	}
	fmt.Println(list)
	return htp.RespOK("", list)
}

/*
 * 描述: 获取题干列表
 * 路由: group/api
 * 方法: GET
 *********************************************************************/
type TopLibSelectList struct {
	CId int64 `form:"cId" json:"cId" binding:"required"` //公司ID
}

func (this *TopLibSelectList) Handle(c *gin.Context, ctx context.Context) htp.Response {
	list := make([]TopLibCount, 0)
	rspSel, err := model.StaffServer.GetProSelectList(ctx, &gpb.BasePageReq{
		CId: this.CId,
	})
	if err != nil {
		return htp.RespModelErr(rspSel.Message, err)
	}
	if len(rspSel.ProbleList) > 0 {

		for _, v := range rspSel.ProbleList {
			rsp, err := model.StaffServer.GetPoolCount(ctx, &gpb.BaseCountReq{
				Id:   v.Id,
				CId:  this.CId,
				Type: 2,
			})
			if err != nil {
				return htp.RespModelErr("", err)
			}
			var topReq TopLibCount
			topReq.Count = rsp.Count
			topReq.Id = v.Id
			topReq.Name = v.Name
			list = append(list, topReq)
		}
	}
	return htp.RespOK("", list)
}
